<?php


namespace StudyBuddy\Controllers;

use \StudyBuddy\WebPage;
use \StudyBuddy\User;


class Activate extends WebPage
{

	protected $aRequired = array('eid', 'hash');

	/**
	 * Object of type User
	 * representing the user whose account is being activated
	 *
	 * @var object of type User
	 */
	protected $oActivatedUser;

	/**
	 *
	 * @var array
	 */
	protected $aEmail;

	protected $layoutID = 1;

	/**
	 * Maximum number of days after which
	 * validation code is no longer valid
	 *
	 * Defaults to 604800 which is 7 days
	 *
	 * @var int
	 */
	protected $timeLimit = 604800;

	protected function main()
	{
		$this->getEmailRecord()
		->validateExpiration()
		->activateUser()
		->setReturn();
	}

	/**
	 * Select one row from EMAILS table
	 *
	 * @return $this
	 */
	protected function getEmailRecord()
	{
		$this->aEmail = $this->oRegistry->Mongo->EMAILS->findOne(array('_id' => $this->oRequest['eid'], 'code' =>  $this->oRequest['hash']));
		if(empty($this->aEmail)){
			throw new \StudyBuddy\Exception('Unable to find record of user');
		}

		d('$this->aEmail: '.print_r($this->aEmail, 1));

		if((!empty($this->aEmail['i_vts'])) && $this->aEmail['i_vts'] > 0){
			throw new \StudyBuddy\Exception('This account has already been activated');
		}

		return $this;
	}


	/**
	 * Make sure that validation code is not
	 * expired
	 *
	 * @todo need to generate new validation code and re-email it
	 * to the same user
	 *
	 * @return object $this
	 */
	protected function validateExpiration()
	{
		if( ($this->aEmail['i_code_ts'] + $this->timeLimit) < time()){
			throw new \StudyBuddy\Exception('Activation code no longer valid');
		}

		return $this;
	}


	/**
	 * Change user's user_group_id to registered
	 * and set validation_time to now in EMAILS record
	 *
	 * @return object $this
	 */
	protected function activateUser()
	{
		$aUser = $this->oRegistry->Mongo->USERS->findOne(array('_id' => (int)$this->aEmail['i_uid']) );

		if(empty($aUser)){
			throw new \StudyBuddy\Exception('Unable to find user, please create a new account');
		}

		$this->oActivatedUser = User::factory($this->oRegistry, $aUser);
		$this->oActivatedUser->activate()->save();

		/**
		 * Now IF Viewer is actually the user that was just activated
		 * we must also update the Viewer!
		 * If we don't then the Viewer object is not updated
		 * and the Viewer in session is still unactivated
		 */
		if($this->oRegistry->Viewer->getUid() === $this->oActivatedUser->getUid()){
			$this->processLogin($this->oActivatedUser);
		}

		$this->oRegistry->Dispatcher->post($this->oActivatedUser, 'onUserUpdate');

		$this->aEmail['i_vts'] = time();
		$this->oRegistry->Mongo->EMAILS->save($this->aEmail);

		return $this;
	}

	/**
	 * @todo translate string
	 *
	 */
	protected function setReturn()
	{
		$this->aPageVars['title'] = 'Account activation complete';

		$this->aPageVars['body'] = '<div id="tools" class="larger">Account activation complete<br/>The account <b>'.$this->oActivatedUser->username.'</b> now has all the privileges<br/>
		of a registered user on our website.<br/>
		<br/>If you not already logged in, please login using<br/>
		the form above</div>';
	}


	protected function sendNewCode()
	{

	}
}
